package main.resources.controller.command.chat;

import java.math.BigInteger;

import main.resources.controller.command.Command;
import main.resources.controller.dao.moodle.MdlChatDao;
import main.resources.controller.dao.moodle.MdlContextDao;
import main.resources.controller.dao.moodle.MdlCourseSectionDao;
import main.resources.exceptions.DaoException;
import main.resources.model.entity.moodle.MdlChat;
import main.resources.model.entity.moodle.MdlContext;
import main.resources.model.entity.moodle.MdlCourseSection;
import main.resources.useful.Useful;

public class ChatRemoveCommand implements Command {

	private MdlChat chat;
	private String id;
	
	public ChatRemoveCommand(String id) {
		this.id = id;
	}

	@Override
	public void execute() throws DaoException {
		this.chat = MdlChatDao.getInstance().find(id);		
		MdlContext context = MdlContextDao.getInstance().lastByLevel(BigInteger.valueOf(70));
		MdlCourseSection section = MdlCourseSectionDao.getInstance().filterByCourse(chat.getCourse()).get(0);
		section.setSequence(Useful.decaySequence(section.getSequence(), context.getInstanceid().toString()));
		MdlCourseSectionDao.getInstance().edit(section);
		MdlContextDao.getInstance().remove(context);
		MdlChatDao.getInstance().remove(chat);
	}

}
